#include "../includes.h"

int WarmupTimeCounter;
int timer_good_to_go = NO;

char MESG[2049];

char TOD_TASK;
char TOD_MAKE_TASK;
char TOD_MAKE_TASK_STEP;

int TimeSliceCounter;

monitor_ monitor;
func_ func;
SYS_ SYSTEM;
GC_ GC;
EVENT_ EVENT;
TEMPER_ TEMPERATURE;
USER_ USER;
CL_ CHECKLOCK;
RT_ TASK;

//====================================================
//	Reset, booting
//====================================================
char First_1PPS;

/*************************************************************/
// manual signal enable
char MANUAL_START;
char Manual_Start_Counter;
void Manual_Start();
/*************************************************************/

//char PROMPT[PROMPT_LENGTH+1] = "GPNDU_XB>";
char PROMPT[PROMPT_LENGTH + 1] = "GRCA_XB>";

void Initialization();
void Init_MAIN();
void Init_STRUCT();

void UpdateTOD();
void UpdateSTATUS();

void PPS_Handling();

/* ---------------------------------------------------------------------
 Function Name : Initialization
 Description
 98-05-02 10:14
 --------------------------------------------------------------------- */
void TaskFunction10ms();
void TaskFunction4ms();
void TaskFunction500ms();
void TaskFunction1000ms();

void send_title();
void send_version();
void Calculation(void);

void TOD_Function();
void TOD_Make_Function();

extern int redirect_printf_to_fifo(char* path);
extern void RCUPOS_SEND();
extern void PopCVData();
extern void VME_Interrupt_handling();
#define IPC_FIFO_PATH			"/etc/ipc/fifo0"

void send_version() {
	sprintf((char*) MESG, "    S/N: %s\r\n\r\n",GetSerialNumber() ); //HardWare_Version);
	TransmitMessage((BYTE*) MESG, PORT_CONTROL);
	sprintf((char*) MESG, "    F/W Ver[%s]\r\n", FirmWare_Version);
	TransmitMessage((BYTE*) MESG, PORT_CONTROL);
	sprintf((char*) MESG, "    FPGA Ver[%s]\r\n", FPGA_Version);
	TransmitMessage((BYTE*) MESG, PORT_CONTROL);
}

void send_title() {
	//==================================================
	// Firmware Version + Hardware Version
	//==================================================
	TransmitMessage(
			(unsigned char*) "\r\n====================================================\r\n"
				"    "HardWareName"\r\n\r\n", PORT_CONTROL);
	send_version();

	TransmitMessage(
			(unsigned char*) "                               NAVICOM CO., LTD.    \r\n"
				"====================================================\r\n"
			HardWareName"("__TIME__")"" Started!!\r\n", PORT_CONTROL);

//	sprintf((char*) MESG, "+5V:%.2fV VCC:%.2fV VEE:%.2fV\r\n", fPlus5V,
//			fPlusVcc, fMinusVee);
//	TransmitMessage((BYTE*) MESG, PORT_CONTROL);
}

int cur_cnt = 0;
int cur_zero_cnt = 0;
int aa=0;
void hw10ms_int_handler(int signum) {
	static int prov_cnt = 0;

	ClearMonitor10msHWInterrupt();

	if (timer_good_to_go == NO) {
		return;
	}

	if (signum == SIGUSR2) {
		//------------------------------------------------
		_TIMER1_Handler(1); //10ms timer handler
		//------------------------------------------------

		SetFPGAmLatchHigh();
		cur_cnt = GetCounterx(4);
		SetFPGAmLatchLow();
		if(iLsiDebugVariable == 4444 ) printf("%3d,cur_cnt : %9d\n",TimeSliceCounter,cur_cnt);//Sewon
		//printf("cur_cnt:%d\n", cur_cnt);
		dbgCV(134) printf("cur_cnt : %d, prov_cnt : %d\n",cur_cnt,prov_cnt);
		if (cur_cnt < prov_cnt) {
			//printf("cur_cnt:%d, prov_cnt:%d\n", cur_cnt, prov_cnt);
			//kang ClearFlag1PPS();

			//if( TimeSliceCounter < 98 ){
			//if( TimeSliceCounter < 99 ){
			if ((TimeSliceCounter < 75) && (SYSTEM.OPERATION.OPMODE
					== OPMODE_OPERATION)) {
				//if( TimeSliceCounter < 90 ){
				LogItString3(LOG_CATA1_NORM, LOG_CATA2_WARN, "Timer error,",
						TimeSliceCounter, prov_cnt, cur_cnt);
			}

			PPS_Handling();
			if(aa++>100)
			{
				dbgCV(134)printf("$$$$$$$$$$$$ hw10ms_int_handler $$$$$$$$$$$$$$\n");
				aa=0;
			}
			cur_zero_cnt = cur_cnt;
			DIAG.Watch1PPSCounter = 0;

		}
		//else{
		//	if((cur_cnt-prov_cnt>1500000) || (cur_cnt == prov_cnt)){
		//		LogItString3("Timer jump",prov_cnt,cur_cnt,(cur_cnt-prov_cnt));
		//	}
		//}

		prov_cnt = cur_cnt;
	}

}

//########################################################################################

TempSen_ ts = { .temp_sel = 0, .get_ocxo = 0, .get_board = 0, .prev_ocxo = 0,
		.prev_board = 0, .ocxo_err_cnt = 0, .board_err_cnt = 0,
		.ocxo_match_cnt = 0, .board_match_cnt = 0, .ocxo_temp = 0.0,
		.board_temp = 0.0, .prev_ocxo_temp = 0.0, .prev_board_temp = 0.0 };

void get_temperature() {

	//return 0;
	//--- OCXO Temperature ------------------------------------------------------------------------
	if (ts.temp_sel) {
		ts.temp_sel = 0;

		ts.get_ocxo = ioctl(fdFPGA, IOCTL_OCXO_TEMP, 0);
		if (temp_sensor_debug) {
			temp_sensor_debug = 0;
			ts.get_ocxo = 0x1fe3;
		}
		//hmlee - won't have any log messages
		//if( fabs( (ts.get_ocxo - ts.prev_ocxo) > 80) && (ts.prev_ocxo != 0) ){
		//	ts.get_ocxo = ts.prev_ocxo;
		//}

		//hmlee - 0 to 150
		if (ts.get_ocxo >= 0x0 && ts.get_ocxo <= 0x960) {
			ts.ocxo_temp = (float) ts.get_ocxo * 0.0625;
			ts.ocxo_err_cnt = 0;
			ts.ocxo_ok_cnt++;
		}
		//hmlee - -1 to -55?
		else if (ts.get_ocxo >= 0x1C90 && ts.get_ocxo <= 0x1FFF) {
			//LogItString1("ocxo temp-b:",ts.get_ocxo);
			//ts.ocxo_temp = (1 + ~ts.get_ocxo) * 0.0625;  //2's complement
			//ts.ocxo_temp = -((~(ts.get_ocxo-1)) * 0.0625);  //2's complement
			ts.ocxo_temp = -(float) (1 + ((~ts.get_ocxo) & 0x00001FFF))
					* 0.0625; //2's complement
			//printf(">>> ocxo: %x, %x, %d, %f, %f\n",
			//ts.get_ocxo,
			//((~ts.get_ocxo)&0x00001FFF),
			//-(1 + ((~ts.get_ocxo)&0x00001FFF) ),
			//-0.123,
			//-(float)(1 + ((~ts.get_ocxo)&0x00001FFF) ) * 0.0625
			//);
			//
			//LogItString1("ocxo temp-a:",(int)ts.ocxo_temp);
			ts.ocxo_err_cnt = 0;
			ts.ocxo_ok_cnt++;
		} else {
			ts.ocxo_ok_cnt = 0;
			if (ts.ocxo_err_cnt == 0) {
				//LogItString1(LOG_CATA1_NORM, LOG_CATA2_WARN,"Under OCXO : Timeout", ts.get_ocxo);
			}
			ts.ocxo_err_cnt++;
			ts.ocxo_temp = ts.prev_ocxo_temp;
		}

		if (ts.get_ocxo == ts.prev_ocxo) {
			ts.ocxo_match_cnt++;
		} else {
			ts.ocxo_match_cnt = 0;
		}

		if (ts.ocxo_match_cnt >= 3600 || ts.ocxo_err_cnt > 60) {//3600->1800sec->30min
			if (ts.ocxo_err == NO) {
				ts.ocxo_err = YES;
				LogItString1(LOG_CATA1_NORM, LOG_CATA2_WARN,
						"Under OCXO : Temperature Sensing Error", ts.get_ocxo);
				//				LogItString1("Under OCXO : TWI SR", ioctl(fdFPGA, IOCTL_TEMP_CONFIG, 0) );
			}
		} else if (ts.ocxo_match_cnt == 0 && ts.ocxo_ok_cnt > 60) {
			if (ts.ocxo_err == YES) {
				ts.ocxo_err = NO;
				LogItString1(LOG_CATA1_NORM, LOG_CATA2_WARN,
						"Under OCXO : Temperature Sensing OK", ts.get_ocxo);
			}
		}

		if (temp_debug) {
			//sprintf((char*)MESG,"ot,%.2d,%.2d,%.2d,,%d,%d,,%.2f,%.2f\n",
			//	ts.ocxo_match_cnt, ts.ocxo_ok_cnt, ts.ocxo_err_cnt,
			//	ts.prev_ocxo, ts.get_ocxo,
			//	ts.prev_ocxo_temp, ts.ocxo_temp
			//);

			sprintf((char*) MESG,
					"temp,now,%3d,%3d, %5.2f,%5.2f ,err,%d,%d,match,%d,%d\n",
					ts.get_board, ts.get_ocxo, ts.board_temp, ts.ocxo_temp,
					ts.board_err_cnt, ts.ocxo_err_cnt, ts.board_match_cnt,
					ts.ocxo_match_cnt);

			TransmitMessage((BYTE*) MESG, PORT_CONTROL);
		}
	}
	//--- Board Temperature ------------------------------------------------------------------------
	else {
		ts.temp_sel = 1;

		ts.get_board = ioctl(fdFPGA, IOCTL_BOARD_TEMP, 0);
		if (temp_sensor_debug) {
			temp_sensor_debug = 0;
			ts.get_board = 0x1fe3;
		}
		//hmlee - won't have any log messages
		//if( fabs( (ts.get_board - ts.prev_board) > 80) && (ts.prev_board != 0) ){
		//	ts.get_board = ts.prev_board;
		//}

		if (ts.get_board >= 0x0 && ts.get_board <= 0x960) {
			ts.board_temp = (float) ts.get_board * 0.0625;
			ts.board_err_cnt = 0;
			ts.board_ok_cnt++;
		} else if (ts.get_board >= 0x1C90 && ts.get_board <= 0x1FFF) {
			//LogItString1("board temp-b:",ts.get_board);
			//ts.board_temp = (1 + ~ts.get_board) * 0.0625;  //2's complement
			//ts.board_temp = -((~(ts.get_board-1)) * 0.0625);  //2's complement
			ts.board_temp = -(float) (1 + ((~ts.get_board) & 0x00001FFF))
					* 0.0625; //2's complement
			//printf(">>> board: %x, %x, %d, %f, %f\n",
			//ts.get_board,
			//((~ts.get_board)&0x00001FFF),
			//-(1 + ((~ts.get_board)&0x00001FFF) ),
			//-0.123,
			//-(float)(1 + ((~ts.get_board)&0x00001FFF) ) * 0.0625
			//);
			//LogItString1("board temp-a:",(int)ts.board_temp);
			ts.board_err_cnt = 0;
			ts.board_ok_cnt++;
		} else {
			ts.board_ok_cnt = 0;
			if (ts.board_err_cnt == 0) {
				//LogItString1(LOG_CATA1_NORM, LOG_CATA2_WARN,						"On Board : Timeout", ts.get_board);
			}
			ts.board_err_cnt++;
			ts.board_temp = ts.prev_board_temp;
		}

		if (ts.get_board == ts.prev_board) {
			ts.board_match_cnt++;
		} else {
			ts.board_match_cnt = 0;
		}

		if (ts.board_match_cnt >= 3600 || ts.board_err_cnt > 60) {//3600->1800sec->30min
			if (ts.board_err == NO) {
				ts.board_err = YES;
				LogItString1(LOG_CATA1_NORM, LOG_CATA2_WARN,
						"On Board : Temperature Sensing Error", ts.get_board);
				//				LogItString1("On Board : TWI SR", ioctl(fdFPGA, IOCTL_TEMP_CONFIG, 0) );
			}
		} else if (ts.board_match_cnt == 0 && ts.board_ok_cnt > 60) {
			if (ts.board_err == YES) {
				ts.board_err = NO;
				LogItString1(LOG_CATA1_NORM, LOG_CATA2_WARN,
						"On Board : Temperature Sensing OK", ts.get_board);
			}
		}

		if (temp_debug) {
			sprintf((char*) MESG,
					"temp,now,%3d,%3d, %5.2f,%5.2f ,err,%d,%d,match,%d,%d\n",
					ts.get_board, ts.get_ocxo, ts.board_temp, ts.ocxo_temp,
					ts.board_err_cnt, ts.ocxo_err_cnt, ts.board_match_cnt,
					ts.ocxo_match_cnt);

			//sprintf((char*)MESG,"bt,%.2d,%.2d,%.2d,,%d,%d,,%.2f,%.2f\n",
			//	ts.board_match_cnt, ts.board_ok_cnt, ts.board_err_cnt,
			//	ts.prev_board, ts.get_board,
			//	ts.prev_board_temp, ts.board_temp
			//);
			TransmitMessage((BYTE*) MESG, PORT_CONTROL);
		}
	}

	ts.prev_ocxo = ts.get_ocxo;
	ts.prev_board = ts.get_board;

	ts.prev_ocxo_temp = ts.ocxo_temp;
	ts.prev_board_temp = ts.board_temp;

	//printf("Board:%.4f, OCXO:%.4f\n", board_temp, ocxo_temp);

	TEMPERATURE.CURRENT = (float) ts.ocxo_temp;
	AverageTemperature(ts.ocxo_temp);

}
//########################################################################################

ocxoid_ OCXOID = { .TraceControl = OFF, .step = 0, .step_cnt = 0,
		.RD_1st = 0.0, .RD_2nd = 0.0,

		.detailSense = 0, .detail = 0, .DetailOffset = 0, .cnt = 0,

		.trace_cnt = 0, .trace_interval = 1, .match_cnt = 0, .ro_sum = 0,
		.aveoffset = 0,

};

void TaskFunction10ms() // 1/100
{
	char sig;

	LogDualExt();
	LogDualDebug(IsSigOn());

	if (func.startup_task) {
		func.startup_task = OFF;
		StartUpProcedure();
	}

	if (func.rb_task) {
		func.rb_task = OFF;
		Rb_Task();
	}

	if (func.e1_task) {
		func.e1_task = OFF;
		//e1_task2();
		//lmu_task();
	}

	if (func.eb_task) {
		func.eb_task = OFF;
		if (CONFIG.RcvType==1)
			EB_Task();
		else {
			SYSTEM.EB.RxCheckCounter++;
			if (SYSTEM.EB.RxCheckCounter > 3) {
				Init_EB(INIT_EB_SILENT, INIT_EB_NOT_RESET, NO);
				//SendCommandToF1000c(nmeaOutput);
			}
		}

		if (SYSTEM.EB.STATUS == EB_STATUS_RUN && reset_eb_done == NO) {
			reset_eb_done = YES;
		}

		if (skipping_change_eb) {
			skipping_change_eb++;

			if (SYSTEM.EB.STATUS == EB_STATUS_RUN) {
				if (skipping_change_eb > 60) {
					skipping_change_eb = 0;
				}
			} else {
				if (skipping_change_eb > 15 * 60) {
					skipping_change_eb = 0;
				}
			}
		}
		//e1_task();

		//if(SYSTEM.EB.STATUS == EB_STATUS_NONE){

		//}
		//else {
		//	SYSTEM.E1.STATUS = E1_STATUS_NONE;
		//}
	}

	if (func.control_task) {
		func.control_task = OFF;

		//hmlee - read 10M counter & deatil counter here
		MonitorNewDetailCounter();
		ReadRawCount(1);

		Control();

		DisplayMessage();

	}

	if (func.pll_task_1) {
		//func.pll_task_1 = OFF;
		if (PLL.TURN == PLL_SECOND) {
			//Start_PLL();
		}
	}

	if (func.pll_task_2) {
		func.pll_task_2 = OFF;
		if (PLL.TURN == PLL_FIRST || PLL.TURN == PLL_THIRD) {
			Start_PLL();
		}
	}

	if (func.start_trace_task) {
		func.start_trace_task = OFF;
		Start_Trace_Control();
	}

	if (func.eb_event_task) {
		func.eb_event_task = OFF;
		EB_Event_Handling();
	}

	if (func.check_lock_task) {
		func.check_lock_task = OFF;
		//CheckLock(); /* Check Lock status after receiving E/B mesages */
		//		if( buf_sync15.iMatch < 10){ CheckF1000cLock(); }
		//		else {  CheckLock(); }//CheckARLock();
		if (CONFIG.RcvType==1)
			CheckLock();
		else
			CheckF1000cLock();

	}

	if (func.diag_task) {
		func.diag_task = OFF;
		Diagnose();
		//eb_10min_off();
		//eb_40min_off();
		//kang printf("TaskFunction10ms()->Diagnose() : Comment\n");
	}

	if (func.config_log_task) {
		func.config_log_task = OFF;
		ConfigLogEvent();
	}

	if (func.counter_shift_task) {
		func.counter_shift_task = OFF;
		//CounterShiftControl();
	}

	if (func.manual_start_task) {
		func.manual_start_task = OFF;
		Manual_Start();
	}

	if (func.scc2_task) {
		func.scc2_task = OFF;
		//SCC2_RECEIVE_HANDLING();
	}

	if (func.led_task) {
		func.led_task = OFF;
		DisplayLED();
	}

	if (func.led_blink_task) {
		func.led_blink_task = OFF;
	}

	//==================================================================
	if (func.autoreply_task) {
		func.autoreply_task = OFF;

		AutoSendSet();

		//kang AutoSendCommand(PORT_COMMON);
	}

	if (AutoReply.ctrl_flag) {
		AutoSendCommand(PORT_CONTROL);
	}

	// 10ms
	if (func.common_port_task) {
		func.common_port_task = OFF;

		if (COMMAND.COMMON_PORT.SERVICE == CMD_EXECUTION) {
			//kang ExecuteCommand(PORT_COMMON);
		}

		//if(COMMAND.COMMON_PORT.SERVICE == CMD_REPLY){
	}

	if (TimeSliceCounter > 101) {

		PPS_Handling();
		//printf("$ 7$");

		//ClearFlag1PPS();
		//ClearFlagDetail();
		if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG_DETAIL) {
			//TransmitMessage((BYTE*)"Cleared by Timer\r\n",PORT_CONTROL);
		}

	}

	sig = ReadMySigStatus();

	if ((SYSTEM.OPERATION.STATUS == STATUS_STANDBY) && (sig == ON)) {
		SetMeActive();
	}

	if (func.is_over10e6_task) {
		func.is_over10e6_task = OFF;
		//IsOver8E6();
	}

	if (func.monitor_cnt_task == ON) {
		func.monitor_cnt_task = OFF;
		ReadDifferenceCount();
	}

	if (func.fpga_tod_task == ON) {
		TOD_OUT_FPGA();
	}

	if (AmIDeleting()) {
		DIAG.me_exist_nok_counter++;
		DIAG.me_exist_ok_counter = 0;
	} else {
		DIAG.me_exist_ok_counter++;
		DIAG.me_exist_nok_counter = 0;
	}

	//hmlee - let's say 1 seconds
	if (DIAG.me_exist_ok_counter > 100) {
		DIAG.me_exist_ok_counter = 200;
		DIAG.i_exist = YES;
	}
	//hmlee - let's say 50 milli
	else if (DIAG.me_exist_nok_counter > 4) {
		DIAG.me_exist_nok_counter = 10;
		DIAG.i_exist = NO;
	}
	//hmlee - else?? sustain
	else {
	}

	if (DIAG.DUAL_STEP == TRY_NULL) {
		if (AreYouExist()) {
			DIAG.you_exist_ok_counter++;
			DIAG.you_exist_nok_counter = 0;
		} else {
			DIAG.you_exist_nok_counter++;
			DIAG.you_exist_ok_counter = 0;
		}
	}

	//hmlee - let's say 1 seconds
	if (DIAG.you_exist_ok_counter > 100) {
		DIAG.you_exist_ok_counter = 200;
		DIAG.u_exist = YES;
	}
	//hmlee - let's say 50 milli
	else if (DIAG.you_exist_nok_counter > 4) {
		DIAG.you_exist_nok_counter = 10;
		DIAG.u_exist = NO;
	}
	//hmlee - else?? sustain
	else {
	}

	if (dual_clear_debug) {
		return;
	}

	ClearFPGAOpenDetect();

}

void TaskFunction100ms(void) {

}

void TaskFunction4ms() {

	switch (COMMAND.CTRL_PORT.SERVICE) {
	case CMD_EXECUTION:
		ExecuteCommand(PORT_CONTROL);
		break;
	case CMD_REPLY:

		ReplyCommand(PORT_CONTROL);
		break;
	}

	switch (COMMAND.COMMON_PORT.SERVICE) {
	case CMD_EXECUTION:
		ExecuteCommand(PORT_COMMON);
		break;
	case CMD_REPLY:
		ReplyCommand(PORT_COMMON);
		break;
	}

}

void TaskFunction500ms() {

}

void TaskFunction1000ms() {
	if (++GC.Second == 3600) {
		GC.Second = 0;
		GC.Hour++;
	}
	if (GC.Hour == 24) {
		GC.Hour = 0;
		GC.Day++;
		SetLEAPSECOND(POLL);
	}

	WarmStart(WARM_START_UPDATE);

	LsiTest();
	task_cmd();
	SetUdpMsg();
	if( lcd_data.cnt_idle > 0 ){
		lcd_data.cnt_idle++;
		if(lcd_data.cnt_idle > 60*2 ) lcd_data.cnt_idle = 0;
	}

	if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG_OFFSET) {
		sprintf(MESG, "gC,%.2lf,gCA,%.2lf,gD,%.2lf,"
			"Ref,%d,gOff1,%.2lf,"
			"C,%d,PE,%d,PJE,%d,PJC,%d,"
			"GB,%.2lf,YB,%.2lf,GA,%.2lf,YA,%.2lf,YVB,%.2lf,YVA,%.2lf,"
			"gID,%.2lf,off,%.2lf,off_s,%.2lf,gOD,%.2lf,gOD_b_c,%.2lf,rcv_off,%.2lf,"
			"_r_o,%.2lf,_r_d,%.2lf,dac,%.2lf,"
			"ATO,%.3E,TO,%.3E\r\n", gCurrent, gCurrentAll, gDetail,
				MyReference, gOffset1, Offset_2nd.Counter, Offset_2nd.PosEnd,
				PhaseJump_EVENT, PhaseJump_Counter, Offset_2nd.Grad_Before,
				Offset_2nd.YInt_Before, Offset_2nd.Grad_After,
				Offset_2nd.YInt_After, Offset_2nd.YValue_Before,
				Offset_2nd.YValue_After, gInputData, Offset_2nd.Offset,
				Offset_2nd.Offset_Sum, gOutputData, gOutputData_Before_Offset,
				Recovery_Offset, _raw_offset, _raw_difference, CONTROL.RealDAC,
				CONTROL.AvgTimeOffset, CONTROL.TimeOffset);
		TransmitMessage((BYTE*) MESG, PORT_CONTROL);
	}

	if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG_DBG) {
		DEBUG_OUT();
	}

	if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG_TIME) {
		DisplayTime();
	}

}

void Calculation(void) {
	char result;

	result = 0;

	if (HoldoverCalc.flag == ON) {
		holdover_calculation();
		result++;
	}

	if (result == 0) {
		EVENT.CALCULATION = OFF;
	}
}

extern int cur_cnt;
extern int cur_zero_cnt;
void TOD_Function() {
	int byte = 0;
	int fpga_cnt = 0;

	if (TimeSliceCounter < 3) {
		return;
	}

	//kang if(tod_tx_ready) return;
	SetFPGAmLatchHigh();
	fpga_cnt = GetCounterx(4);
	SetFPGAmLatchLow();

	if (fpga_cnt < 300000) {
		return;
	}

	if (TOD.printon == ON) {
		sprintf((char*) MESG,
				"%d,%5d,%2d,%7d,%7d,%7d,V,%d,EB,%d:%d:%d %2d:%2d:%2d,TOD,%d:%d:%d %2d:%2d:%2d,"
					"S,%02X,E,%02X,O,%02X,I,%02X\r\n",
				SYSTEM.TIME.TOD_MismatchCounter, SYSTEM.TIME.TOD_MatchCounter,
				TimeSliceCounter, fpga_cnt, cur_cnt, cur_zero_cnt,
				SYSTEM.TIME.Valid, SYSTEM.EB.TIME.YEAR, SYSTEM.EB.TIME.MONTH,
				SYSTEM.EB.TIME.DAY, SYSTEM.EB.TIME.HOUR, SYSTEM.EB.TIME.MINUTE,
				SYSTEM.EB.TIME.SECOND, SYSTEM.TIME.TOUT.YEAR,
				SYSTEM.TIME.TOUT.MONTH, SYSTEM.TIME.TOUT.DAY,
				SYSTEM.TIME.TOUT.HOUR, SYSTEM.TIME.TOUT.MINUTE,
				SYSTEM.TIME.TOUT.SECOND, TOD.status, TOD.error, TOD.opmode,
				TOD.inform);
		TransmitMessage((BYTE*) MESG, PORT_CONTROL);
	}
dbgCV(3365)	printf("%d, %d, %d, %d, %d\n",TOD.turn,SYSTEM.TIME.Valid,SYSTEM.TIME.TOUT.SECOND,SYSTEM.IO.CTRL_PORT.MODE,TOD.SKIP_TOD_TIMER);
	if ((TOD.turn == ON && SYSTEM.TIME.Valid)) {
		// at every second
		//if( !(SYSTEM.TIME.CUR.SECOND&0x01) ){
		//if (!(SYSTEM.TIME.TOUT.SECOND & 0x01)) {	//12-05-18 sewon
			if (SYSTEM.IO.CTRL_PORT.MODE != UDOWNLOAD) {
				if (TOD.SKIP_TOD_TIMER == 0) {

					//if(!DoesU_TODOccupied()){
					//	SetTODOccupied();
					//TOD_OUT(PORT_TOD, BINARY);
					TOD_OUT(PORT_TOD, UART_CONTROL);

					//}
				} else {
					TOD.SKIP_TOD_TIMER--;
				}
			}
		//}
	} else {
	}

	//=======================================================
	//	2. Control Port
	//=======================================================
	switch (SYSTEM.IO.CTRL_PORT.MODE) {
	case UTOD:
		TOD_OUT(PORT_CONTROL, BINARY);
		break;
	case UTOD_ASCII:
		TOD_OUT(PORT_CONTROL, ASCII);
		break;
	case USTATUS_ASCII:

		if (SYSTEM.TIME.Valid) {
			//if(!(SYSTEM.TIME.CUR.SECOND&0x01)){
			if (!(SYSTEM.TIME.TOUT.SECOND & 0x01)) {
				STATUS_OUT(PORT_CONTROL, ASCII);
			}
		} else {
			if (!(GC.RunningTime & 0x01)) {
				STATUS_OUT(PORT_CONTROL, ASCII);
			}
		}
		break;
	case USTATUS_BINARY:
		STATUS_OUT(PORT_CONTROL, BINARY);
		break;

	case USTATUS_TEXT:
		STATUS_OUT(PORT_CONTROL, TEXT);
		break;

	case UDEBUG5:
		if( CONFIG.RcvType==1 )EB_STATUS_OUT();
		break;

	}

	//=======================================================
	//	3. Common Port
	//=======================================================
	switch (SYSTEM.IO.COMMON_PORT.MODE) {
	case UTOD:
		TOD_OUT(PORT_COMMON, BINARY);
		break;
	case UTOD_ASCII:
		TOD_OUT(PORT_COMMON, ASCII);
		break;
	case USTATUS_ASCII:
		if (SYSTEM.TIME.Valid) {
			//if(!(SYSTEM.TIME.CUR.SECOND&0x01)){
			if (!(SYSTEM.TIME.TOUT.SECOND & 0x01)) {
				STATUS_OUT(PORT_COMMON, ASCII);
			}
		} else {
			if (!(GC.RunningTime & 0x01)) {
				STATUS_OUT(PORT_COMMON, ASCII);
			}
		}
		break;
	}

	if (SYSTEM.TIME.Valid || TEMP_TOD) {
		IncrementTOD();
		TEMP_TOD_MADE = ON;
		if (LogStartFlag == ON && Setting_LOG_Flag == ON) {
			LogStartFlag = OFF;
			//LogIt(LOG_RESET,GC.RunningTime);

			//LogIt(LOG_CATA1_NORM, LOG_CATA2_UNSORTED, LOG_RESET,0);
			LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, "Time valid");

		}
	}
	SYSTEM.TIME.TOUT = SYSTEM.TIME.CUR;
	increase_second(&SYSTEM.TIME.TOUT);
	UpdateTOD();

	//hmlee - make TOD for fpga out
	memcpy(&F_TOD.rtod, &R_TOD, sizeof(R_TOD_));

	TOD_TASK = OFF;
}

void Manual_Start() {

	Manual_Start_Counter++;

	switch (Manual_Start_Counter) {

	case 1:
		USER.MANUAL_CONTROL = ON;
		CONTROL.RealDAC = CONTROL.ControlDAC = (double) CONFIG.DAC;
		break;
	case 2:
		EVENT.PLL = ON;
		break;
	case 5:
		CONTROL.SYNC = ON;
		break;
	case 12:
		CONTROL.EVEN_SYNC = ON;
		break;
	case 13:
		UnconditionalSigOn();

		SYSTEM.TIME.GPS = SYSTEM.EB.TIME;

		MANUAL_START = OFF;
		Manual_Start_Counter = 0;
		break;
	}
}

void PPS_Handling() {
	//	ntp_time_get();
	prevLogTimeOut++;

	/*

	 if(CONFIG.beep_flag){
	 if(!beep_alarm_flag){
	 beep_pps_flag = ON;
	 EnableBeep();
	 }
	 }

	 */

	if (SYSTEM.TIME.Valid && TOD_TASK == ON) {
		LogIt(LOG_CATA1_NORM, LOG_CATA2_WARN, LOG_TOD_Correction, 0);
	}
	TOD_TASK = ON;

	EVENT.OnePPS1 = ON;
	EVENT.OnePPS2 = ON;

	TimeSliceCounter = 0;

	GC.RunningTime++;

	if (First_1PPS) {
		First_1PPS = OFF;
		CONTROL.SYNC = ON;
		control_sync_type = 4;
	}

}

void TOD_Make_Function() {
	//	if(TOD_MAKE_TASK_STEP == 0){
	//
	//		//if( SYSTEM.EB.STATUS == EB_STATUS_RUN ){
	//			StartTOD();
	//		//}
	//		TOD_MAKE_TASK_STEP = 1;
	//	}
	//
	//	else if(TOD_MAKE_TASK_STEP == 1){
	//		if( SYSTEM.TIME.Valid || TEMP_TOD ){
	//			IncrementTOD();
	//			TEMP_TOD_MADE = ON;
	//			if(LogStartFlag == ON && Setting_LOG_Flag == ON){
	//				LogStartFlag = OFF;
	//				//LogIt(LOG_RESET,GC.RunningTime);
	//
	//				LogIt(LOG_RESET,0);
	//
	//			}
	//		}
	//		TOD_MAKE_TASK_STEP = 2;
	//    }
	//    else if(TOD_MAKE_TASK_STEP == 2 ){
	//
	//		//printf("TOD_MAKE_TASK_STEP == 2\n");
	//
	//		UpdateTOD();
	//    	TOD_MAKE_TASK_STEP = 3;
	//    }
	//
	//	else if(TOD_MAKE_TASK_STEP == 3){
	//
	//		//printf("TOD_MAKE_TASK_STEP == 3\n");
	//
	//		if( SYSTEM.OPERATION.OPMODE != OPMODE_POWERUP){
	////			SYSTEM.EB.rxflag = NO;
	//		}
	//		TOD_MAKE_TASK_STEP = 4;
	//
	//	}
	//	else if(TOD_MAKE_TASK_STEP == 4){
	//
	//		//printf("TOD_MAKE_TASK_STEP == 4\n");
	//
	//		UpdateSTATUS();
	//		TOD_MAKE_TASK_STEP = 0;
	//    	TOD_MAKE_TASK = OFF;
	//	}

	if (TOD_MAKE_TASK_STEP == 0) {
		StartTOD();
		TOD_MAKE_TASK_STEP = 1;
	} else if (TOD_MAKE_TASK_STEP == 1) {
		UpdateSTATUS();
		TOD_MAKE_TASK_STEP = 0;
		TOD_MAKE_TASK = OFF;
	}
}

void Initialization() {
	Init_STARTUP();
	Init_MAIN();
	Init_MAINFUNC();
	Init_CONTROL();

	Init_EB(INIT_EB_LOUD, INIT_EB_FIRST, NO);
	Init_EB2(INIT_EB_LOUD, INIT_EB_FIRST, NO);

	Init_VME();

	Init_CTRLFUNC();
	Init_SCC();
	Init_EBCMD();
	Init_COMMAND();
	Init_DIAG();

	//kang ---> Down.c	Init_DOWNLOAD();

	//Init_LOG();

	LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, "########## PowerUp started");

	Init_CONFIG();
	//kang ---> Help.c	Init_HELP();
	Init_OSC();
	Init_TOD();

	Init_STATUS();

	Init_DUAL();

	//Init_COUNTER();

	Init_PLL();
	Init_POSITION();
	Init_POSITION2();	//OEMV1 sewon
	Init_LED(fdFPGA);
	//Init_RTC();

	Init_E1();
	Init_rb();

	Init_LMU();

	Init_Offset_2nd();

	Init_cfg();
	init_udpd();
	//InitDisplayLEDgpndu(fdFPGA);

	//led_act_grn_blk();
	//led_act_grn_off();

	//led_lock_grn_blk();

	//led_gps_red_blk();
	//led_gps_grn_blk();

	//led_alm_red_blk();
	

}

void Init_MAIN() {
	First_1PPS = YES;

	TimeSliceCounter = 0;

	GC.Second = 0;
	GC.Hour = 0;
	GC.Day = 0;
	GC.ACCsecond = 0;
	GC.RunningTime = 0;

	Init_STRUCT();

	/******************************************/
	MANUAL_START = OFF;
	Manual_Start_Counter = 0;
	/******************************************/

	TOD_TASK = OFF;
	TOD_MAKE_TASK = OFF;
	TOD_MAKE_TASK_STEP = 0;

	func.common_port_task = OFF;
	func.autoreply_task = OFF;
	func.startup_task = OFF;
	func.eb_task = OFF;
	func.control_task = OFF;
	func.temperature_task = OFF;
	func.pll_task_1 = OFF;
	func.pll_task_2 = OFF;
	func.start_trace_task = OFF;
	func.eb_event_task = OFF;
	func.check_lock_task = OFF;
	func.diag_task = OFF;
	func.config_log_task = OFF;
	func.counter_shift_task = OFF;
	func.manual_start_task = OFF;
	func.scc2_task = OFF;
	func.led_task = OFF;
	func.led_blink_task = OFF;
	func.e1_task = OFF;
	func.rb_task = OFF;
	func.is_over10e6_task = OFF;
	func.parameter = 0;

	// dual channel
	monitor.dual = OFF;
	monitor.eb = OFF;
	monitor.holdover = OFF;
	monitor.debug = OFF;

}

void Init_STRUCT() // EVENT, SYSTEM, USER
{
	/* EVENT */
	EVENT.OnePPS1 = OFF;
	EVENT.OnePPS2 = OFF;
	EVENT.TIMER1 = OFF;
	EVENT.TIMER2 = OFF;
	EVENT.PLL = OFF;
	EVENT.SCC1 = OFF;
	EVENT.SCC1_TX = OFF;
	EVENT.SCC2 = OFF;
	EVENT.SCC3_TX = OFF;
	EVENT.SCC3 = OFF;
	EVENT.SCC4 = OFF;
	EVENT.SCC5 = OFF;
	EVENT.SCC6 = OFF;
	EVENT.CALCULATION = OFF;

	SYSTEM.ID = UNDEFINED;
	SYSTEM.MODE = UNDEFINED;

	/* SYSTEM->OPERATION */

	SYSTEM.OPERATION.OPMODE = OPMODE_POWERUP;
	//hmlee - don't put here
	//we will have two 1s
	//  1> 0000/00/00 00:00:00 000 xxxx
	//  1> 0000/00/00 00:00:00 008 xxxx
	//    LogItString("PowerUp");


	//    SYSTEM.OPERATION.OPMODE = OPMODE_WARMUP;

	SYSTEM.OPERATION.LOCKMODE = LOCKED;
	SYSTEM.OPERATION.STATUS = STATUS_BLOCK;
	SYSTEM.OPERATION.SELFTEST = 0;
	SYSTEM.OPERATION.CTRLMODE = CTRL_LOCKEDtoGPS;

	/* SYSTEM->IO */

//	SYSTEM.IO.CTRL_PORT.MODE 		= UDEBUG;
	//SYSTEM.IO.CTRL_PORT.MODE = UCOMMAND;
	SYSTEM.IO.CTRL_PORT.PROMPT = ON; /* command or tod or status */
	SYSTEM.IO.CTRL_PORT.COMMAND_SET = NAVICOM_COMMAND;
	SYSTEM.IO.CTRL_PORT.Timer = 0;
	SYSTEM.IO.CTRL_PORT.Print = OFF;
	SYSTEM.IO.CTRL_PORT.Odtcmd = OFF;

	SYSTEM.IO.COMMON_PORT = SYSTEM.IO.CTRL_PORT;

	SYSTEM.IO.COMMON_PORT.MODE = ODT_COMMAND;

	/* USER control */
	USER.MANUAL_HOLDOVER = OFF;
	USER.MANUAL_CONTROL = OFF;
	USER.MANUAL_SIG_ON = OFF;

	/* TASK */
	TASK.T10ms = OFF;
	TASK.T100ms = OFF;
	TASK.T4ms = OFF;
	TASK.T500ms = OFF;
	TASK.T1000ms = OFF;
	/* Counter */
	TASK.C10ms = 0;
	TASK.C10msLED = 0;
	TASK.C4ms = 0;
	TASK.C500ms = 0;

}

int VME_init_count=OFF;
int iWdtClearCnt=0;
int _main(int fd) {
	FILE *qfp;
	key_t key;
	int pck1 = 0;
	int retry = 0;
	unsigned iSendRCUPOS=0;

	ResetPLL();
	ResetPLL();
	ResetPLL();
	ResetPLL();
	ResetPLL();
	ResetPLL();
	ResetPLL();
	ResetPLL();
	ResetPLL();
	ResetPLL();
	ResetPLL();

	InitPLL_1st(0, 0, 0);

	InitDual();

	SetMeNotActive();SetMeSwitchNotReady();ClearMyFail();
	ClearFail();
	Debug_Halt(1);
	VME_init_count=ON;
	Initialization();
	Debug_Halt(2);

	Setting_CONFIG();

	Init_VERSION();
	Debug_Halt(3);
	Debug_Halt(4);

	//hmlee - show who we are
	send_title();
	SendPrompt();

	//hmlee - show configuration
	Show_Config();

	//hmlee - if que0 not found
	if (access(MSGQid0Path, R_OK)) {
		//hmlee - create one
		if ((qfp = fopen(MSGQid0Path, "w"))) {
			fclose(qfp);
		}
	}

	//hmlee - que0 open
	IPC.msgQid0 = -1;
	if ((key = ftok(MSGQid0Path, 'B')) == -1) {
		printf("Que0 Ftok Error... %s\n", MSGQid0Path);
	}

	if ((IPC.msgQid0 = msgget(key, IPC_CREAT | 0666)) == -1) {
		printf("Que0 Command MessageQ msgget Error \n");
	} else
		printf("Que0 msgKey Id#:%d\n", IPC.msgQid0);

	//hmlee - open shm
	ntp_shm_open();
	//set_tty( fdEB, B38400, 1 );
	mclock_init();

	if (CONFIG.RcvType==1 ) {
		set_tty(fdEB, B115200, 1);  //9600
		printf("Set Receiver type:Sync15\n");
	}
	else if (CONFIG.RcvType==2 ) {
		set_tty(fdEB, B115200, 1);  //9600
		printf("Set Receiver type:Ublox\n");
	}

	else {
		//set_tty( fdEB, B115200, 1 );
		//set_tty(fdEB, B115200, 1);

		set_tty( fdEB, B115200, 1 );
		printf("Set Receiver type:OEMV2\n");
		set_tty( fdEB2, B115200, 2 );
		printf("Set Receiver type:OEMV1\n");
	}

	//-----------------------------------------------------------
	//WDT Clock Enable(WDT, FAN alarm)---------------------------
	//-----------------------------------------------------------
	pck1 = ioctl(fdFPGA, IOCTL_PCK1_ENABLE, 0);
	printf("pck1 register : %d\n", pck1);
	if (pck1 == 24) {
		printf("pck1 enable good!\n");
	} else {
		printf("pck1 enable bad!\n");
	}
	//-----------------------------------------------------------

	//set4GValue(10000000);

	if (redirect_printf_to_fifo(IPC_FIFO_PATH) < 0) {
		printf("redirect output fail\n");
	}

	set_udpd();

	ResetTODFpga();ResetTODFpga();

	//hmlee - can't trust
	SelTodFPGA();SelTodFPGA();SelTodFPGA();

	timer_good_to_go = YES;

	do {
		SetFpgaValue(47,19);
		pck1 = (GetFpgaValue(47) & 0xff);

		if (pck1 == 19) {
			break;
		}

	} while (++retry < 5);

	if (retry >= 5) {
		sprintf(MESG, "Fpga 47, 0x%x", pck1);
		LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
	}

//	char *EBcmd[10];
//
//	EBcmd[0] = "unlogall\n";
//	EBcmd[1] = "log satxyza ontime 15\n";
//	EBcmd[2] = "log rangea ontime 15\n";
//
//	SendMessageToEB(EBcmd[0], strlen(EBcmd[0])); // kay
//	SendMessageToEB(EBcmd[1], strlen(EBcmd[1])); // kay
//	SendMessageToEB(EBcmd[2], strlen(EBcmd[2])); // kay
//	SendMessageToEB(EBcmd[3], strlen(EBcmd[3])); // kay
//	SendMessageToEB(EBcmd[4], strlen(EBcmd[4])); // kay

//	ForceEBRun();

	init_st_bd();
	//oemv3_msg_rcvd( temp_buf, 0, len, SDATA);
	sprintf(MESG,"#RCUPOS,%f,%f,%f*xxx\r\n",CONFIG.PosX,CONFIG.PosY,CONFIG.PosZ);
	oemv3_msg_rcvd( MESG, 0, strlen(MESG), SDATA, fdEB);
	for (;;) {
		//sleep(1);
		Monitor10msHWInterrupt();
		if( iWdtClearCnt ++ > M_THRESHOLD_WDT_CLEAR){
			iWdtClearCnt=0;
			clear_Wdt(0);
		}
		udpd();

		if (EVENT.OnePPS2) {
			EVENT.OnePPS2 = OFF;
			ntp_time_send();
		}

		if (EVENT.SCC3) {
			EVENT.SCC3 = OFF;
			SCC13_RECEIVE_HANDLING(PORT_CONTROL);
		}

		//-----------------------
		//kang 08-12-01
//		SendEBHandler(fdEB);
		ReceiveEBHandler(fdEB);
		//------------------------
		//-----------------------
		//Sewon 12-04-05	OEMV1
//		SendEBHandler(fdEB2);
		ReceiveEBHandler(fdEB2);
		//-----------------------
		//Sewon 12-05-07	Rb-SRO-100
//		SendEBHandler(fdRbSRO);
		ReceiveEBHandler(fdRbSRO);
		//------------------------


		//----------------------------------------
		//Timing Job
		//----------------------------------------
		if (TASK.T10ms) { // 10ms -> 0.01sec
			TASK.T10ms = OFF;
			TaskFunction10ms();

			//if( TASK.C10ms % 10 == 0 ){ //100ms
			//	DisplayLEDgpndu();
			//}
			//if( TASK.C10ms % 10 == 0 ){ //100ms
			//	DisplayLEDgpndu();
			//}

			if (TASK.C10msLED % 20 == 0) { //250ms
				TASK.C10msLED = 0;
				//DisplayLEDgpndu();
			}
			DisplayHandler();
			//TGainDisplay();
		}

		if (TASK.T100ms) {
			TASK.T100ms = OFF;
			TaskFunction100ms();
		}

		if (TASK.T4ms) { // 100ms -> 0.04sec
			TASK.T4ms = OFF;
			TaskFunction4ms();
		}

		if (TASK.T500ms) { // 500ms -> 0.5sec
			TASK.T500ms = OFF;
			//kang TaskFunction500ms();
			//kang printf("TaskFunction500ms() : Comment\n");
			//EVENT.SCC3 = ON;
		}

		if (TASK.T1000ms) { // 1sec
			TASK.T1000ms = OFF;
			TaskFunction1000ms();

			if(iSendRCUPOS++%10==0)
			{
				RCUPOS_SEND();
			}

			//WDT Clear-----------
			if (!wdt_debug) {
				ClearFPGAWdt();
			}
			//--------------------

			//--------------------------------------------------------------------
			//Get temperature data from (On board, Side OCXO) temperature sensor
			get_temperature();
			//--------------------------------------------------------------------


		}

		if (VME.T1000ms) {
			//sewon vme func
			VME_Function();
		}
		// CV Master에서 CV Data 전송 함수
			PopCVData();


		//-------------------------------------------
		//Tx Buffer [2000-02-02 11:43]
		//-------------------------------------------

		if (CalcDetail.event) {
			calc_detail_table();
		}

		if (EVENT.CALCULATION) {
			Calculation();
		}

		if (TOD_TASK == ON) {
			TOD_Function();
		}

		if (TOD_MAKE_TASK == ON) {
			TOD_Make_Function();
		}

		//if( buf_f1000c.CheckOutTimeOut && buf_sync15.iMatch < 10){
		if (CONFIG.RcvType==1) {
			buf_sync15.CheckOutTimeOut = 0;
			MonitorGPSinput(&buf_sync15);
		}
		//if( buf_sync15.CheckOutTimeOut && buf_f1000c.iMatch < 10){
		//if(	CONFIG.RcvType ){
		else {
			buf_f1000c.CheckOutTimeOut = 0;
			buf_oemv1.CheckOutTimeOut = 0;
			buf_RbSRO.CheckOutTimeOut = 0;
			MonitorGPSinput(&buf_f1000c,fdEB);
			MonitorGPSinput(&buf_oemv1,fdEB2);//sewon
			MonitorGPSinput(&buf_RbSRO,fdRbSRO);//sewon
		}

	}
	//if( IPC.msgQid0 != -1 ) msgctl(IPC.msgQid0, IPC_RMID, 0);

	return 0;
}


void clear_Wdt(int signo)
{
		ClearFPGAWdt();
		if( iLsiDebugVariable == 51 )printf("clear wdt\n");
}


static struct timeval  __mclock_start;

void mclock_init( void )
{
    gettimeofday( &__mclock_start, NULL );
}

long int mclock( void )
{
    struct timeval timecurrent;
    struct timeval timeresult;

    gettimeofday( &timecurrent, NULL );

    timeresult.tv_sec  = timecurrent.tv_sec  - __mclock_start.tv_sec;
    timeresult.tv_usec = timecurrent.tv_usec - __mclock_start.tv_usec;

    if( timeresult.tv_usec < 0 )
    {
        timeresult.tv_sec--;
        timeresult.tv_usec += 1000000;
    }

    return timeresult.tv_sec * 1000 + ( timeresult.tv_usec / 1000 );
}

long int prevUs=0;
int b10msIntError1st = 0;
long int i10msIntErrorCnt = 0;
#define M_10ms_Monitor_Interval 10 //10ms


void ClearMonitor10msHWInterrupt()
{
	b10msIntError1st = 0;
	i10msIntErrorCnt = 0;
}

void Monitor10msHWInterrupt()
{
	long int Us;
	Us = mclock();
	if( Us - prevUs > M_10ms_Monitor_Interval ){
		if( i10msIntErrorCnt ++ > ( 1000 / M_10ms_Monitor_Interval )*3 ){
			_TIMER1_Handler(1);
			if( !b10msIntError1st ){
				printf("-----(%ld) %ld ------\n",Us,i10msIntErrorCnt);
				LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "10ms HW Int. Error by OCXO fault");
			}
			b10msIntError1st = 1 ;
		}
		//printf(">>-----(%ld) %ld ------\n",Us,i10msIntErrorCnt);
		prevUs = Us;
	}
//	SetBitLow(12,wbD4);//sewon 100Hz->1Hz ->100Hz, sewon
//	dbgCV(145)	printf("set>> 0x%02X( %d ) : 0x%02X( %d )\n",12,12,0x00,0x00);//sewon

}
void RCUPOS_SEND()
{
	if( (CONFIG.iCVType == 0) && (VME.Data.TimeSync_Mode == VME_CV_MODE) && (VME.Data.Geodetic_Mode==ON) ){			//master mode && Geodetic mode
		//Position : Geodetic Value//
		sprintf(MESG,"#RCUPOS,%f,%f,%f*xxx\r\n",CONFIG.PosX,CONFIG.PosY,CONFIG.PosZ);

		if(CONFIG.NETSEND==1)	net_send(MESG, strlen(MESG),0);
		else if(CONFIG.NETSEND==0){
			VME_CV_write(MESG,strlen(MESG));	//VME Master signal	->	slave blockram write
		}
		dbgCV(129) printf("%s,size:%d\nCV_RCUPOS_Send:%s\n",(VME.Data.Geodetic_Mode==ON)?"Geo_Mode":"Not Geo_Mode",strlen(MESG),MESG);
	}

	else if( (CONFIG.iCVType == 0) && (VME.Data.TimeSync_Mode == VME_CV_MODE) && (VME.Data.Geodetic_Mode==OFF) ){	//master mode && NOT Geodetic mode
		//Position : Average Value//
		sprintf(MESG,"#RCUPOS,%f,%f,%f*xxx\r\n",CONFIG.PosX,CONFIG.PosY,CONFIG.PosZ);

		if(CONFIG.NETSEND==1)	net_send(MESG, strlen(MESG),0);
		else if(CONFIG.NETSEND==0){
			VME_CV_write(MESG,strlen(MESG));	//VME Master signal	->	slave blockram write
		}
		dbgCV(129) printf("%s,size:%d\nCV_RCUPOS_Send:%s\n",(VME.Data.Geodetic_Mode==ON)?"Geo_Mode":"Not Geo_Mode",strlen(MESG),MESG);
	}

	if( (CONFIG.iCVType == 1) && (VME.Data.TimeSync_Mode == VME_CV_MODE) && (VME.Data.Geodetic_Mode==ON) ){			//slave mode && Geodetic mode
		//Position : Geodetic Value//
		sprintf(MESG,"#RCUPOS,%f,%f,%f*xxx\r\n",CONFIG.PosX,CONFIG.PosY,CONFIG.PosZ);
		oemv3_msg_rcvd( MESG, 0, strlen(MESG), SDATA, fdEB);

		dbgCV(129) printf("%s,size:%d\nCV_RCUPOS_Send:%s\n",(VME.Data.Geodetic_Mode==ON)?"Geo_Mode":"Not Geo_Mode",strlen(MESG),MESG);
	}
	else if( (CONFIG.iCVType == 1) && (VME.Data.TimeSync_Mode == VME_CV_MODE) && (VME.Data.Geodetic_Mode==OFF) ){	//slave mode && NOT Geodetic mode
		//Position : Average Value//
		//sprintf(MESG,"#RCUPOS,%f,%f,%f*xxx\r\n",CONFIG.PosX,CONFIG.PosY,CONFIG.PosZ);
		//oemv3_msg_rcvd( MESG, 0, strlen(MESG), SDATA, fdEB);

		dbgCV(129) printf("%s,size:%d\nCV_RCUPOS_Send:%s\n",(VME.Data.Geodetic_Mode==ON)?"Geo_Mode":"Not Geo_Mode",strlen(MESG),MESG);
	}

	if(CONFIG.VMEMODE){
		sprintf(MESG,"#RCUPOS,%f,%f,%f*xxx\r\n",CONFIG.PosX,CONFIG.PosY,CONFIG.PosZ);

		if(CONFIG.NETSEND==1)	net_send(MESG, strlen(MESG),0);
		else if(CONFIG.NETSEND==0){
			VME_CV_write(MESG,strlen(MESG));	//VME Master signal	->	slave blockram write
		}
		dbgCV(129) printf("%s,size:%d\nCV_RCUPOS_Send:%s\n",(VME.Data.Geodetic_Mode==ON)?"Geo_Mode":"Not Geo_Mode",strlen(MESG),MESG);

	}
}
